package cn.stylefeng.guns.modular.rm.service;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.stylefeng.guns.modular.rm.entity.RmGame;
import cn.stylefeng.guns.modular.rm.entity.RmGameScore;
import cn.stylefeng.guns.modular.rm.mapper.RmGameMapper;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description TODO
 * @Author xiesm
 * @Date 2024/4/17 16:45
 **/
@Service
public class RmGameService extends ServiceImpl<RmGameMapper, RmGame> {
    @Autowired
    private RmGameMapper rmGameMapper;

    public List<String> getGameids() {
        return getGameids(null, null);
    }

    public List<String> getGameids(Date beginDate, Date endDate) {
        LambdaQueryWrapper<RmGame> lambdaQuery = new LambdaQueryWrapper<>();
        if (beginDate != null) {
            lambdaQuery.ge(RmGame::getDate, beginDate);
        }
        if (endDate != null) {
            lambdaQuery.lt(RmGame::getDate, endDate);
        }
        List<RmGame> rmGames = rmGameMapper.selectList(lambdaQuery);
        return rmGames.stream().map(RmGame::getGameid).collect(Collectors.toList());
    }

    public String getGame(String data) {
        Document document = Jsoup.parse(data);
        Elements gameList = document.getElementsByClass("game_list");
        Element game = gameList.get(0);
        Elements lis = game.select("li");

        List<RmGame> games = new ArrayList<>();

        for (int i = 0; i < lis.size(); i++) {
            Element li = lis.get(i);
            String onclick = li.attr("onclick");
            String gameid = onclick.substring(onclick.indexOf("'") + 1, onclick.lastIndexOf("'"));
            Elements gameTeams = li.getElementsByClass("game_team");
            String teamA = gameTeams.get(0).text();
            String teamB = gameTeams.get(1).text();

            String gameName = li.getElementsByClass("game_name").text();
            Integer num = gameName.lastIndexOf(" ");

            String time = gameName.substring(0, num);
            String name = gameName.substring(num);

            DateTime date = DateUtil.parse("2024-" + time, "yyyy-MM-dd HH:mm");

            RmGame rmGame = RmGame.builder()
                    .league(name)
                    .gameid(gameid)
                    .date(date)
                    .teamA(teamA)
                    .teamB(teamB)
                    .build();
            games.add(rmGame);
        }
        List<String> ids = getGameids();
        List<RmGame> saveList = new ArrayList<>();
        for (int i = 0; i < games.size(); i++) {
            RmGame rmGame = games.get(i);
            LambdaQueryWrapper<RmGame> lambdaQuery = new LambdaQueryWrapper<>();
            lambdaQuery.eq(RmGame::getGameid, rmGame.getGameid());
            if (!ids.contains(rmGame.getGameid())) {
                saveList.add(rmGame);
            }
        }
        this.saveBatch(saveList);
        return null;
    }

    public static String getGames(String cookie, String type, String gameid) {
        JSONObject params = new JSONObject();
        params.put("Cookie", cookie);
        params.put("type", type);
        String url = null;
        switch (type) {
            case "0":
                url = "http://124.222.75.100:8089/cp/rmmarket?cookie={}&type={}";
                url = StrUtil.format(url, cookie, type);
                break;
            case "1":
                url = "http://124.222.75.100:8089/cp/rmmarket?cookie={}&type={}&gameid={}";
                url = StrUtil.format(url, cookie, type, gameid);
                break;
        }
        ThreadUtil.sleep(1000);

        String body = HttpUtil.createGet(url)
                .execute().body();
        return body;
    }
}
